package cn.edu.zut.sunday.news.domain;


import cn.edu.zut.sunday.base.domain.BaseEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.Objects;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.Hibernate;

@Setter
@Getter
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "tbl_news")
public class News extends BaseEntity<Integer> {

    @Column(length = 255)
    String title;

    @Column(length = 512)
    String source;

    @Column(length = 128)
    String headImgUrl;

    @Column(length = 128)
    String publishTime;

    @Column(length = 128)
    String url;

    @JsonIgnore
    @ManyToOne(optional=false)//可选属性optional=false,表示categoryId不能为空。删除新闻，不影类别；
    @JoinColumn(name="category")//设置在News表中的关联字段(外键)
    private NewsCategory newsCategory;//所属类别

    //@Column(length = 128)
    //private String category;//所属类别

    @Column(length = 128)
    String keyword;

    @Column(length = 128)
    String tag;

    @Column(length = 512)
    String description;

    @Column(length = 1024)
    String content;

    @Override
    public boolean equals(Object o) {
        if (this == o) {return true;}
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(
          o)) {return false;}
        News news = (News) o;
        return id != null && Objects.equals(id, news.id);
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}
